package com.ruibang.glass.material.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.material.constant.Constants;
import com.ruibang.glass.material.constant.MaterialWareHouseCode;
import com.ruibang.glass.material.domain.req.MaterialKineSideWareHouseReq;
import com.ruibang.glass.material.dto.RqStatusStaticsAllDTO;
import com.ruibang.glass.material.dto.RqStatusStaticsDTO;
import com.ruibang.glass.material.dto.WareHouseAndLocation;
import com.ruibang.glass.material.dto.WareHouseInfoDTO;
import com.ruibang.glass.material.entity.MaterialLineSideWarehouseLocation;
import com.ruibang.glass.material.entity.MaterialWarehouseInfo;
import com.ruibang.glass.material.entity.rq.MaterialATypeWaterBottleRackManagement;
import com.ruibang.glass.material.mapper.GMaterialLineSideWarehouseLocationMapper;
import com.ruibang.glass.material.service.MaterialLineSideWarehouseLocationService;
import com.ruibang.glass.material.service.MaterialWarehouseInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 线边仓库位表 服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-04-07
 */
@Service
public class GMaterialLineSideWarehouseLocationServiceImpl extends ServiceImpl<GMaterialLineSideWarehouseLocationMapper, MaterialLineSideWarehouseLocation> implements MaterialLineSideWarehouseLocationService {

    @Autowired
    private MaterialWarehouseInfoService materialWarehouseInfoService;

    /**
     * 线边仓库位表 服务实现类
     *
     * @param
     * @return
     */
    @Override
    public IPage<MaterialLineSideWarehouseLocation> getPage(MaterialKineSideWareHouseReq materialKineSideWareHouseReq) {
        LambdaQueryWrapper<MaterialLineSideWarehouseLocation> queryWrapper = Wrappers.<MaterialLineSideWarehouseLocation>lambdaQuery();
        if (StringUtils.isNotBlank(materialKineSideWareHouseReq.getAssociatedContainerStatus())) {
            queryWrapper.eq(MaterialLineSideWarehouseLocation::getAssociatedContainerStatus, materialKineSideWareHouseReq.getAssociatedContainerStatus());
        }
        if (StringUtils.isNotBlank(materialKineSideWareHouseReq.getLocationCode())) {
            queryWrapper.like(MaterialLineSideWarehouseLocation::getLocationCode, materialKineSideWareHouseReq.getLocationCode());
        }
        if (StringUtils.isNotBlank(materialKineSideWareHouseReq.getLocationName())) {
            queryWrapper.like(MaterialLineSideWarehouseLocation::getLocationName, materialKineSideWareHouseReq.getLocationName());
        }
        if (StringUtils.isNotBlank(materialKineSideWareHouseReq.getStatus())) {
            queryWrapper.eq(MaterialLineSideWarehouseLocation::getStatus, materialKineSideWareHouseReq.getStatus());
        }
        if (StringUtils.isNotBlank(materialKineSideWareHouseReq.getWareHouseCode())) {
            queryWrapper.eq(MaterialLineSideWarehouseLocation::getWarehouseCode, materialKineSideWareHouseReq.getWareHouseCode());
        }
        Page<MaterialLineSideWarehouseLocation> materialWarehouseInfoPage = baseMapper.selectPage(new Page<>(materialKineSideWareHouseReq.getPageNo(), materialKineSideWareHouseReq.getPageSize()),
                queryWrapper.orderByDesc(MaterialLineSideWarehouseLocation::getCreatedDate)
        );
        Map<String, String> materialWarehouseInfoMap = new HashMap<>();
        List<MaterialWarehouseInfo> materialWarehouseInfoList = materialWarehouseInfoService.list(Wrappers.<MaterialWarehouseInfo>lambdaQuery().select(MaterialWarehouseInfo::getId, MaterialWarehouseInfo::getWarehouseName));
        for (MaterialWarehouseInfo materialWarehouseInfo : materialWarehouseInfoList) {
            materialWarehouseInfoMap.put(materialWarehouseInfo.getId(), materialWarehouseInfo.getWarehouseName());
        }

        if (materialWarehouseInfoPage.getRecords() != null && materialWarehouseInfoMap != null) {
            for (MaterialLineSideWarehouseLocation materialLineSideWarehouseLocation : materialWarehouseInfoPage.getRecords()) {
                if (materialWarehouseInfoMap.get(materialLineSideWarehouseLocation.getWarehouseId()) != null) {
                    materialLineSideWarehouseLocation.setWarehouseName(materialWarehouseInfoMap.get(materialLineSideWarehouseLocation.getWarehouseId()));
                }
            }
        }
        return materialWarehouseInfoPage;
    }

    @Override
    public void updateMaterialLineSideWarehouseLocation(MaterialLineSideWarehouseLocation materialLineSideWarehouseLocation) {
        List<MaterialLineSideWarehouseLocation> materialLineSideWarehouseLocations = baseMapper.selectBatchIds(materialLineSideWarehouseLocation.getIds());
        for (MaterialLineSideWarehouseLocation materialLineSideWarehouseLocation1 : materialLineSideWarehouseLocations) {
            materialLineSideWarehouseLocation1.setStatus(materialLineSideWarehouseLocation.getStatus());
            baseMapper.updateById(materialLineSideWarehouseLocation1);
        }

    }

    /**
     * 线边仓库位表 服务实现类
     *
     * @param
     * @return
     */
    @Override
    public List<WareHouseAndLocation> getAllWareHouseAndLocation() {
        List<WareHouseAndLocation> wareHouseAndLocationList = new ArrayList<>();
        List<MaterialLineSideWarehouseLocation> materialLineSideWarehouseLocations = baseMapper.selectList(Wrappers.<MaterialLineSideWarehouseLocation>lambdaQuery().eq(MaterialLineSideWarehouseLocation::getStatus, Constants.ONE_STR));
        Map<String, WareHouseAndLocation> wareHouseAndLocationMap = new HashMap<>();
        for (MaterialLineSideWarehouseLocation materialLineSideWarehouseLocation : materialLineSideWarehouseLocations) {
            if (wareHouseAndLocationMap.get(materialLineSideWarehouseLocation.getWarehouseName()) == null) {
                WareHouseAndLocation wareHouseAndLocation = new WareHouseAndLocation();
                wareHouseAndLocation.setWarehouseCode(materialLineSideWarehouseLocation.getWarehouseCode());
                wareHouseAndLocation.setWareHouseName(materialLineSideWarehouseLocation.getWarehouseName());
                List<WareHouseAndLocation.WareHouseAndLocationDTO> list = new ArrayList<>();
                WareHouseAndLocation.WareHouseAndLocationDTO wareHouseAndLocationDTO = new WareHouseAndLocation.WareHouseAndLocationDTO();
                wareHouseAndLocationDTO.setId(materialLineSideWarehouseLocation.getId());
                wareHouseAndLocationDTO.setLocation(materialLineSideWarehouseLocation.getLocationName());
                list.add(wareHouseAndLocationDTO);
                wareHouseAndLocation.setWareHouseAndLocationDTOList(list);
                wareHouseAndLocationMap.put(materialLineSideWarehouseLocation.getWarehouseName(), wareHouseAndLocation);
            } else {
                WareHouseAndLocation wareHouseAndLocation = wareHouseAndLocationMap.get(materialLineSideWarehouseLocation.getWarehouseName());
                if (!CollectionUtil.isEmpty(wareHouseAndLocation.getWareHouseAndLocationDTOList())) {
                    List<WareHouseAndLocation.WareHouseAndLocationDTO> wareHouseAndLocationDTOList = new ArrayList<>();
                    WareHouseAndLocation.WareHouseAndLocationDTO wareHouseAndLocationDTO = new WareHouseAndLocation.WareHouseAndLocationDTO();
                    wareHouseAndLocationDTO.setId(materialLineSideWarehouseLocation.getId());
                    wareHouseAndLocationDTO.setLocation(materialLineSideWarehouseLocation.getLocationName());
                    wareHouseAndLocationDTOList.add(wareHouseAndLocationDTO);
                    wareHouseAndLocation.setWareHouseAndLocationDTOList(wareHouseAndLocationDTOList);
                    wareHouseAndLocationMap.put(materialLineSideWarehouseLocation.getWarehouseName(), wareHouseAndLocation);
                } else {
                    List<WareHouseAndLocation.WareHouseAndLocationDTO> wareHouseAndLocationDTOList = wareHouseAndLocation.getWareHouseAndLocationDTOList();
                    WareHouseAndLocation.WareHouseAndLocationDTO wareHouseAndLocationDTO = new WareHouseAndLocation.WareHouseAndLocationDTO();
                    wareHouseAndLocationDTO.setId(materialLineSideWarehouseLocation.getId());
                    wareHouseAndLocationDTO.setLocation(materialLineSideWarehouseLocation.getLocationName());
                    wareHouseAndLocationDTOList.add(wareHouseAndLocationDTO);
                    wareHouseAndLocation.setWareHouseAndLocationDTOList(wareHouseAndLocationDTOList);
                    wareHouseAndLocationMap.put(materialLineSideWarehouseLocation.getWarehouseName(), wareHouseAndLocation);
                }
            }
        }
        for (String name : wareHouseAndLocationMap.keySet()) {
            WareHouseAndLocation wareHouseAndLocation = wareHouseAndLocationMap.get(name);
            wareHouseAndLocationList.add(wareHouseAndLocation);
        }
        return wareHouseAndLocationList;
    }

    /**
     * 校验出库申请 数量和库存数量比较
     *
     * @param
     * @return
     */
    @Override
    public List<WareHouseInfoDTO> getAllWareHouseInfoDTO() {
        List<WareHouseInfoDTO> wareHouseInfos = new ArrayList<>();
        for (MaterialWareHouseCode ele : MaterialWareHouseCode.values()) {
            WareHouseInfoDTO wareHouseInfoDTO = WareHouseInfoDTO.builder()
                    .wareHouseCode(ele.getMsg())
                    .wareHouseName(ele.getCode())
                    .build();
            wareHouseInfos.add(wareHouseInfoDTO);
        }
        return wareHouseInfos;
    }

    /**
     * 校验出库申请 数量和库存数量比较
     *
     * @param
     * @return
     */
    @Override
    public List<WareHouseInfoDTO> getLocationItemList() {
        List<WareHouseInfoDTO> wareHouseInfos = new ArrayList<>();
        for (MaterialWareHouseCode ele : MaterialWareHouseCode.values()) {
            WareHouseInfoDTO wareHouseInfoDTO = WareHouseInfoDTO.builder()
                    .wareHouseCode(ele.getMsg())
                    .wareHouseName(ele.getType())
                    .build();
            wareHouseInfos.add(wareHouseInfoDTO);
        }
        return wareHouseInfos;
    }

    @Override
    public List<RqStatusStaticsAllDTO> getRqStatus() {
        List<RqStatusStaticsDTO> rqStatus = baseMapper.getRqStatus();
        Map<String, List<RqStatusStaticsDTO>> collect = new HashMap<>();
        for (RqStatusStaticsDTO rqStatusStaticsDTO : rqStatus) {
            if (StringUtils.isEmpty(rqStatusStaticsDTO.getAssociatedContainerStatus())) {
                if (collect.get("") != null) {
                    List<RqStatusStaticsDTO> rqStatusStaticsDTOS = collect.get("");
                    rqStatusStaticsDTOS.add(rqStatusStaticsDTO);
                    collect.put("", rqStatusStaticsDTOS);
                } else {
                    List<RqStatusStaticsDTO> rqStatusStaticsDTOList = new ArrayList<>();
                    rqStatusStaticsDTOList.add(rqStatusStaticsDTO);
                    collect.put("", rqStatusStaticsDTOList);
                }
            } else {
                if (collect.get(rqStatusStaticsDTO.getAssociatedContainerStatus()) != null) {
                    List<RqStatusStaticsDTO> rqStatusStaticsDTOS = collect.get(rqStatusStaticsDTO.getAssociatedContainerStatus());
                    rqStatusStaticsDTOS.add(rqStatusStaticsDTO);
                    collect.put(rqStatusStaticsDTO.getAssociatedContainerStatus(), rqStatusStaticsDTOS);
                } else {
                    List<RqStatusStaticsDTO> rqStatusStaticsDTOList = new ArrayList<>();
                    rqStatusStaticsDTOList.add(rqStatusStaticsDTO);
                    collect.put(rqStatusStaticsDTO.getAssociatedContainerStatus(), rqStatusStaticsDTOList);
                }

            }
        }
        List<RqStatusStaticsAllDTO> rqStatusStaticsAllDTOList = new ArrayList<>();
        for (String status : collect.keySet()) {
            RqStatusStaticsAllDTO rqStatusStaticsDTO = new RqStatusStaticsAllDTO();
            rqStatusStaticsDTO.setAssociatedContainerStatus(status);
            rqStatusStaticsDTO.setRqStatusStaticsDTOList(collect.get(status));
            rqStatusStaticsAllDTOList.add(rqStatusStaticsDTO);
        }
        return rqStatusStaticsAllDTOList;
    }

    @Override
    public List<RqStatusStaticsDTO> getRqStatusList(MaterialKineSideWareHouseReq materialKineSideWareHouseReq) {
        return baseMapper.getRqStatusList(materialKineSideWareHouseReq);
    }

    @Override
    public List<MaterialLineSideWarehouseLocation> getLocationList(MaterialKineSideWareHouseReq
                                                                           materialKineSideWareHouseReq) {
        return baseMapper.getLocationList(materialKineSideWareHouseReq);
    }
}
